<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <title>
        修改优惠券
    </title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="format-detection" content="telephone=no">
    <link rel="stylesheet" href="../../../backstage/css/x-admin.css" media="all">
</head>

<body>
<div class="x-body">
    <form id="addItem" class="layui-form layui-form-pane"  enctype="multipart/form-data">
        <div class="layui-tab layui-tab-brief" lay-filter="docDemoTabBrief">
            <div class="layui-tab-content">
                <div class="layui-tab-item layui-show">
                    <input type="hidden" name="id" value="{$art.id}">

                    <div class="layui-form-item">
                        <label class="layui-form-label">
                            <span class='x-red'>*</span>优惠券名称
                        </label>
                        <div class="layui-input-block">
                            <input id="itemName" type="text" name="itemName" required="" lay-verify="required"autocomplete="off" class="layui-input-add" maxlength="20"  >
                        </div>
                    </div>

                    <div class="layui-form-item">
                        <label class="layui-form-label">
                            <span class='x-red'>*</span>生效时长(天)
                        </label>
                        <div class="layui-input-block">
                            <input id="howLong" type="number" name="howLong" required="" lay-verify="required" autocomplete="off" class="layui-input-add"
                                   oninput = "if(value>99999)value=99999;value=value.replace(/[^\d]/g,'')" >
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">
                            <span class='x-red'>*</span>价格(元)
                        </label>
                        <div class="layui-input-block">
                            <input id="price" type="number" name="price" required="" lay-verify="required"
                                   autocomplete="off" class="layui-input-add"
                                   oninput = "if(value>99999)value=99999;value=value.replace(/[^\d]/g,'')">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">
                            <span class='x-red'>*</span>状态
                        </label>
                        <div class="layui-input-inline"  >
                            <input id="oldstate" type="text" value="" name="oldstate" autocomplete="off" class="layui-input-add" disabled="disabled" >
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">
                            <span class='x-red'>*</span>修改状态
                        </label>
                        <div class="layui-input-block"  >
                            <select name="category_id" id="state">
                                <option value="">---请选择状态---</option>
                                <option value="有效">有效</option>
                                <option value="已下架">已下架</option>
                            </select>
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">
                            <span class='x-red'>*</span>消耗积分
                        </label>
                        <div class="layui-input-block">
                            <input id="credit" type="number" name="credit"  required="" lay-verify="required" autocomplete="off" class="layui-input-add"  oninput="if(value>99999)value=99999;value=value.replace(/[^\d]/g,'')" >
                        </div>
                    </div>

                    <div class="layui-form-item">
                        <label class="layui-form-label">
                            <span class='x-red'>*</span>描述
                        </label>
                        <div class="layui-input-block">
                            <textarea id="description"  name="description" required=""
                                      class="layui-input-add"  maxlength="100"
                                      style="height:100px;" >
                                </textarea>
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">
                            <span class='x-red'>*</span>使用说明
                        </label>
                        <div class="layui-input-block">
                            <input id="howUse" type="text" name="howUse" required="" lay-verify="required" autocomplete="off" class="layui-input-add" maxlength="30">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">
                            <span class='x-red'></span>备注
                        </label>
                        <div class="layui-input-block">
                            <input id="remark" type="text" name="remark" autocomplete="off" class="layui-input-add" maxlength="30"  >
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <input id="edit" type="button" class="layui-btn"  lay-submit="edit" value="修改">
                    </div>
                    <div style="height:100px;"></div>
                </div>

            </div>
        </div>
    </form>
</div>
    </div>
    <script src="../../../backstage/lib/layui/layui.js" charset="utf-8"></script>
    <script src="../../../backstage/js/x-layui.js" charset="utf-8"></script>
    <script>
    layui.use(['form', 'layer'], function() {
        $ = layui.jquery;
        var form = layui.form(),
            layer = layui.layer;

        $(document).ready(function() {
            //获取传递过来的localStorage
            var id = window.localStorage.getItem('msg');
            console.log(id);
            $.ajax({
                type: 'POST',
                url: '/member/memberManagement/update',
                dataType: "json",
                data: {id: id},
                error: function () {
                    console.log("失败");
                },
                success: function (result) {

                    console.log(result);

                    $("#itemName").val(result.couponName);
                    $("#oldstate").val(result.state);
                    $("#howLong").val(result.howLong);
                    $("#price").val(result.price);
                    $("#credit").val(result.credit);
                    $("#remark").val(result.remark);
                    $("#description").val(result.description);
                    $("#howUse").val(result.howUse);

                    form.verify({
                        howLong: function (value) {
                            if (value.length <= 0) {
                                return '使用时长不能为空';
                            }
                        }
                    });
                    form.verify({
                        price: function (value) {
                            if (value.length <= 0) {
                                return '价格不能为空';
                            }
                        }
                    });
                    form.verify({
                        credit: function (value) {
                            if (value.length <= 0) {
                                return '积分不能为空';
                            }
                        }
                    });
                    form.verify({
                        description: function (value) {
                            if (value.length <= 0) {
                                return '描述不能为空';
                            }
                        }
                    });
                    form.verify({
                        howUse: function (value) {
                            if (value.length <= 0) {
                                return '如何使用不能为空';
                            }
                        }
                    });
                    $("#edit").click(function () {

                        var couponName = $.trim($("#itemName").val());

                        var state = $.trim($("#state").val());
                        var howLong = $.trim($("#howLong").val());
                        var price = $.trim($("#price").val());
                        var credit = $.trim($("#credit").val());
                        var remark = $.trim($("#remark").val());
                        var description = $.trim($("#description").val());
                        var howUse = $.trim($("#howUse").val());


                        if (state == "") {
                            layer.msg('请选择状态', {icon: 5, time: 2000});
                            return false;
                        }

                        if (couponName == "") {
                            layer.msg('商品名不能为空', {icon: 5, time: 2000});
                            return false;
                        }
                        if (howLong == "") {
                            layer.msg('使用时长不能为空', {icon: 5, time: 2000});
                            return false;
                        }
                        if (price == "") {
                            layer.msg('价格不能为空', {icon: 5, time: 2000});
                            return false;
                        }
                        if (credit == "") {
                            layer.msg('积分不能为空', {icon: 5, time: 2000});
                            return false;
                        }
                        if (description == "") {
                            layer.msg('描述不能为空', {icon: 5, time: 2000});
                            return false;
                        }
                        if (howUse == "") {
                            layer.msg('使用方式不能为空', {icon: 5, time: 2000});
                            return false;
                        }
                        $.ajax({
                            type: 'POST',
                            url: '/member/memberManagement/updateCoupon',
                            data: {id:id,
                                couponName: couponName, description: description,
                                howLong: howLong, price: price,
                                credit: credit, howUse: howUse, remark: remark, state: state
                            },
                            dataType: "json",

                            error: function () {
                                alert("失败");
                            },
                            success: function (data) {
                                console.log(data);
                                if (data) {
                                    alert("修改成功");
                                } else {
                                    alert("修改失败");
                                }
                                location.reload();
                            }
                        });
                    });
                }
            });
        });
    });
    </script>
</body>
</html>